package com.wzcl.app.controller.student;


import com.wzcl.app.dao.model.Delivery;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.DeliveryDto;
import com.wzcl.app.model.vo.DeliveryVo;
import com.wzcl.app.service.DeliveryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping("/userDelivery")
@Api(value = "订单物流管理controller", tags = {"订单物流管理接口"})
public class UserDeliveryController {

    @Autowired
    private DeliveryService service;

    @ApiOperation("查看物流订单")
    @PostMapping("/get")
    public RVO<DeliveryVo> getDelivery(@RequestBody DeliveryDto deliveryDto){
        deliveryDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        Delivery delivery = service.getDelivery(deliveryDto);
        if(delivery != null) {
            return RVO.success(DeliveryVo.create(delivery));
        }
        return RVO.success(null);
    }

    @ApiOperation("查看物流订单List")
    @PostMapping("/getList")
    public RVO<List<DeliveryVo>> getDeliveryList(@RequestBody DeliveryDto deliveryDto){
        deliveryDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        List<Delivery> list = service.getDeliveryList(deliveryDto);
        if(list != null && list.size()>0){
            return RVO.success(list.stream().map(DeliveryVo::create).collect(Collectors.toList()));
        }
        return RVO.success(null);
    }

    @ApiOperation("查看物流订单Page")
    @PostMapping("/getPage")
    public RVO<List<DeliveryVo>> getDeliveryPage(@RequestBody DeliveryDto deliveryDto){
        deliveryDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        MyPage<Delivery> Page = service.getDeliveryPage(deliveryDto);
        if(Page != null && Page.getContent().size()>0){
            return RVO.success(Page.getContent().stream().map(DeliveryVo::create).collect(Collectors.toList()));
        }
        return RVO.success(null);
    }

    @ApiOperation("查询物流信息")
    @PostMapping("/queryInfo")
    public RVO<String> queryDeliveryInfo(@RequestBody DeliveryDto deliveryDto){
        deliveryDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        String string = service.queryDeliveryInfo(deliveryDto);
        return RVO.success(string);
    }

    @ApiOperation("确认收货")
    @PostMapping("/confirm")
    public RVO<DeliveryVo> confirmDelivery(@RequestBody DeliveryDto deliveryDto){
        deliveryDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        Delivery delivery = service.confirm(deliveryDto.getId(),deliveryDto.getUserPhone());
        return RVO.success(DeliveryVo.create(delivery));
    }
}
